<?
class Booking extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		$this->load->helper('cookie');
		$this->load->library('encrypt');
	}
	function index()
	{
		
		
	}
	function book($rt_id, $date, $amount, $booked='')
	{
		if ( $this->encrypt->decode(get_cookie('m_name')) != get_cookie('mName') )
			header('location:http://'.$_SERVER['HTTP_HOST'].'/hotelbooking/index.php/login');

		if ( $rt_id == '' || $date == '' || $amount == '' )
			header('location:http://'.$_SERVER['HTTP_HOST'].'/hotelbooking/index.php/');



		$config = $this->config->item('db_config');
		$this->load->model('Dbmodel', '', $config);

		$temp_date = explode('_', $date);

		$from_date = $temp_date[0];
		$to_date = $temp_date[1];

		$data['checkin'] = $from_date;
		$data['checkout'] = $to_date;
		$data['date'] = $date;
		$data['amount'] = $amount;
	

		$objRowHotel = $this->Dbmodel->select_join("hotel", "room_type", "", "room_type.hotel_id = hotel.hotel_id", array('rt_id' => $rt_id));

		if($objRowHotel)
		{
			$data['summaryHotelBooking'] = array(
					 'hotel_id' => $objRowHotel[0]->hotel_id ,
					 'hotel_name' => $objRowHotel[0]->hotel_name ,
					'hotel_description' => $objRowHotel[0]->hotel_description ,
					'hotel_city' => $objRowHotel[0]->hotel_city ,
					'hotel_location' => $objRowHotel[0]->hotel_location ,
					'hotel_address' => $objRowHotel[0]->hotel_address ,
					'rt_id' => $objRowHotel[0]->rt_id ,
					'rt_name' => $objRowHotel[0]->rt_name ,
					'rt_description' => $objRowHotel[0]->rt_description ,
					'rt_price' => $objRowHotel[0]->rt_price ,
				);
			
		}

		$data['booked'] = false;

		// book hotel now
		if( $booked == 'booked' )
		{
				$s_day = (int)substr(trim($from_date), 0, 2);
				$e_day = (int)substr(trim($to_date), 0, 2);

		

				$amount_date = $e_day - $s_day;

				$idMember = $this->Dbmodel->select_where('member', array('mem_user' => $this->encrypt->decode(get_cookie('m_name'))), 'mem_id');

				$objRoom = $this->Dbmodel->select_where('room_type', array('rt_id' => $rt_id), 'hotel_id,rt_quantity');

				$room_rest = (int)$objRoom[0]->rt_quantity - (int)$amount;

				$this->Dbmodel->update_entry('room_type', array('rt_id' => $rt_id), array('rt_quantity' => $room_rest) );

				$insertBook  = array(
				   'rt_id' => $rt_id ,		
				   'hotel_id' => $objRoom[0]->hotel_id ,
				   'booking_date_start' => strtotime($from_date) ,
				   'booking_date_end' => strtotime($to_date) ,
				   'booking_amount_date' => $amount_date ,
				   'client_id' => $idMember[0]->mem_id ,
				   'booking_amount_room' => $amount ,
					'create_on' => strtotime("now")
				);
				$this->Dbmodel->insert_entry('booking', $insertBook);
				$data['booked'] = true;
		}
		$this->load->view('booked_view.php', $data);
	}
}
?>